package com.bcmusic.order.controller;

import com.bcmusic.music.feign.MusicFeign;
import com.bcmusic.music.pojo.Music;
import com.bcmusic.order.config.TokenDecode;
import com.bcmusic.order.pojo.OrderCart;
import com.bcmusic.order.pojo.OrderDetail;
import com.bcmusic.order.service.OrderCartService;
import com.bcmusic.order.service.OrderDetailService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@ApiIgnore
@RestController
@RequestMapping("/orderDetail")
public class OrderDetailController {

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private OrderCartService orderCartService;

    @Autowired
    private MusicFeign musicFeign;

    @Autowired
    private TokenDecode tokenDecode;

    /***
     * OrderDetail分页条件搜索实现
     * @param orderDetail
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) OrderDetail orderDetail, @PathVariable  int page, @PathVariable  int size){
        //调用OrderDetailService实现分页条件查询OrderDetail
        PageInfo<OrderDetail> pageInfo = orderDetailService.findPage(orderDetail, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * OrderDetail分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用OrderDetailService实现分页查询OrderDetail
        PageInfo<OrderDetail> pageInfo = orderDetailService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param orderDetail
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<OrderDetail>> findList(@RequestBody(required = false) OrderDetail orderDetail){
        //调用OrderDetailService实现条件查询OrderDetail
        List<OrderDetail> list = orderDetailService.findList(orderDetail);
        return new Result<List<OrderDetail>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用OrderDetailService实现根据主键删除
        orderDetailService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改OrderDetail数据
     * @param orderDetail
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody OrderDetail orderDetail, @PathVariable Integer id){
        //设置主键值
        orderDetail.setOrderDetailId(id);
        //调用OrderDetailService实现修改OrderDetail
        orderDetailService.update(orderDetail);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增OrderDetail数据
     * @param orderDetail
     * @return
     */
    @PostMapping
    public Result add(@RequestBody OrderDetail orderDetail){
        //调用OrderDetailService实现添加OrderDetail
        orderDetailService.add(orderDetail);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询OrderDetail数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<OrderDetail> findById(@PathVariable Integer id){
        //调用OrderDetailService实现根据主键查询OrderDetail
        OrderDetail orderDetail = orderDetailService.findById(id);
        return new Result<OrderDetail>(true,StatusCode.OK,"查询成功",orderDetail);
    }

    /***
     * 查询OrderDetail全部数据
     * @return
     */
    @GetMapping
    public Result<List<OrderDetail>> findAll(){
        //调用OrderDetailService实现查询所有OrderDetail
        List<OrderDetail> list = orderDetailService.findAll();
        return new Result<List<OrderDetail>>(true, StatusCode.OK,"查询成功",list) ;
    }

//    @PostMapping("testA")
//    public Result<List<OrderDetail>> test(@RequestParam List<Integer> ids){
//        List<OrderCart> carts = orderCartService.finishOrder(ids);
//        List<OrderDetail> orderDetailList = orderDetailService.finishOrder(carts, 30);
//        return new Result<List<OrderDetail>>(true, StatusCode.OK,"查询成功",orderDetailList) ;
//
//        //TODO测试购物车详细数据放到redis
//    }

    /**
     * 查看已经购买的音乐数据，并且有效的
     * @return
     */
    @GetMapping("findBoughtMusic/{pageNum}/{pageSize}")
    public Result<PageInfo<List<OrderDetail>>> findBoughtMusic(@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize")Integer pageSize)
    {
        String userNum = tokenDecode.getUserInfo().get("id");
        OrderDetail orderDetail=new OrderDetail();
        orderDetail.setUserNumber(userNum);
        orderDetail.setStatus(1);
        PageInfo<OrderDetail> page = orderDetailService.findPage(orderDetail, pageSize, pageNum-1);
        return new Result<>(true,StatusCode.OK,"查询成功",page);
    }

    /**
     * 查看授权凭证
     * @param orderDetailId
     * @return
     */
    @PostMapping("getCertificate")
    public Result<String> getCertificate(@RequestParam("orderDetailId") Integer orderDetailId)
    {
        String userNum = tokenDecode.getUserInfo().get("id");
        OrderDetail orderDetail = orderDetailService.findById(orderDetailId);
        if(orderDetail==null||orderDetail.getStatus().equals(0))
        {
            return new Result<>(true,StatusCode.ERROR,"授权信息已过期或者您没有该信息的授权，请检查后重试");
        }
        List<String> musicNumList=new ArrayList<>();
        musicNumList.add(orderDetail.getMusicNumber());
        Music music = musicFeign.findMusicByNum(orderDetail.getMusicNumber()).getData();
        String certificate = orderDetailService.getCertificate(userNum, orderDetail, music);

        return new Result<>(true,StatusCode.OK,"查询成功",certificate);
    }

    /**
     * 根据订单id查询所关联到的订单id信息
     * @param orderId
     * @return
     */
    @PostMapping("gerOrderDetail")
    public Result<List<OrderDetail>>gerOrderDetail(@RequestParam Integer orderId)
    {
        OrderDetail orderDetail=new OrderDetail();
        orderDetail.setOrderId(orderId);
        List<OrderDetail> list = orderDetailService.findList(orderDetail);
        return new Result<>(true,StatusCode.OK,"查询成功",list);
    }

}
